package com.koron.order.application.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.koron.order.application.bean.dto.ApplicationDTO;
import com.koron.order.application.bean.po.ApplicationEquipment;
import com.koron.order.application.feign.DispatchStatusFeignService;
import com.koron.order.application.mapper.ApplicationEquipmentMapper;
import com.koron.order.application.service.ApplicationEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 检修申请-设备信息从表(ApplicationEquipment)表服务实现类
 *
 * @author jzp
 * @since 2021-11-02 09:34:34
 */
@Slf4j
@Service
public class ApplicationEquipmentServiceImpl implements ApplicationEquipmentService {

    @Autowired
    private DispatchStatusFeignService dispatchStatusFeignService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ApplicationEquipment getById(SessionFactory factory, String id) {
        ApplicationEquipmentMapper mapper = factory.getMapper(ApplicationEquipmentMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ApplicationEquipment> list(SessionFactory factory, ApplicationEquipment applicationEquipment) {
        ApplicationEquipmentMapper mapper = factory.getMapper(ApplicationEquipmentMapper.class);
        return mapper.selectList(applicationEquipment);
    }

    private List<ApplicationEquipment> listByAppId(SessionFactory factory, String applicationId) {
        ApplicationEquipment applicationEquipment = new ApplicationEquipment();
        applicationEquipment.setApplicationId(applicationId);
        return list(factory, applicationEquipment);
    }

    @Override
    @TaskAnnotation("listByAppIds")
    public List<ApplicationEquipment> listByAppIds(SessionFactory factory, List<String> appIds) {
        ApplicationEquipmentMapper mapper = factory.getMapper(ApplicationEquipmentMapper.class);
        return mapper.selectListByApplicationId(appIds);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ApplicationEquipment applicationEquipment) {
        ApplicationEquipmentMapper mapper = factory.getMapper(ApplicationEquipmentMapper.class);
        return mapper.insertOrUpdate(applicationEquipment) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<ApplicationEquipment> applicationEquipments) {
        if (CollectionUtils.isEmpty(applicationEquipments)) {
            return false;
        }
        ApplicationEquipmentMapper mapper = factory.getMapper(ApplicationEquipmentMapper.class);
        return mapper.insertOrUpdateBatch(applicationEquipments) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ApplicationEquipment applicationEquipment) {
        ApplicationEquipmentMapper mapper = factory.getMapper(ApplicationEquipmentMapper.class);
        return mapper.update(applicationEquipment) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        ApplicationEquipmentMapper mapper = factory.getMapper(ApplicationEquipmentMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByApplicationId")
    public boolean removeByApplicationId(SessionFactory factory, String applicationId) {
        if (StrUtil.isEmpty(applicationId)) {
            return false;
        }
        ApplicationEquipment classesShift = new ApplicationEquipment();
        classesShift.setApplicationId(applicationId);
        List<ApplicationEquipment> list = list(factory, classesShift);

        return deleteBatchIds(factory, list.stream().map(ApplicationEquipment::getId).collect(Collectors.toList()));
    }
    @Override
    public void updateStatus(SessionFactory factory, ApplicationDTO applicationDTO) {
        Assert.notNull(applicationDTO.getApplicationTime(), "检修申请实际作业信息不能为空");
        String applicationId = applicationDTO.getId();
        String orderType = applicationDTO.getOrderType();
        String content = applicationDTO.getContext();
        int status = 1;
        String statusStr = "可用";
        Date date = applicationDTO.getApplicationTime().getFactStartTime(); // 默认为实际开始时间
        if (orderType == null) {
            // 可用
            status = 1;
            statusStr = "可用";
            // 到可用状态，为实际结束时间，否则是实际开始时间
            date = applicationDTO.getApplicationTime().getFactEndTime();
        } else if ("0".equals(orderType)) {
            // 正常维护
            status = 6;
            statusStr = "正常维护";
        } else if ("1".equals(orderType)) {
            // 故障检修
            status = 4;
            statusStr = "故障检修";
        } else if ("2".equals(orderType)) {
            // 大修
            status = 3;
            statusStr = "大修";
        } else if ("3".equals(orderType)) {
            // 计划
            status = 5;
            statusStr = "计划";
        }

        ApplicationEquipment applicationEquipment = new ApplicationEquipment();
        applicationEquipment.setApplicationId(applicationId);
        applicationEquipment.setEquipmentStatus(Integer.toString(status));
        ApplicationEquipmentMapper mapper = factory.getMapper(ApplicationEquipmentMapper.class);
        mapper.updateByApplicationId(applicationEquipment);

        List<ApplicationEquipment> applicationEquipments = listByAppId(factory, applicationId);
        if (CollectionUtils.isEmpty(applicationEquipments)) {
            return;
        }

        String format = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
        for (ApplicationEquipment equipment : applicationEquipments) {
            if (equipment.getEquipmentStatus() == null) {
                equipment.setEquipmentStatus("1");
            }
            log.info("录入状态，申请单:id{}，设备id:{}，状态:{}", applicationId, equipment.getEquipmentId(), statusStr);
            dispatchStatusFeignService.changeStatus(applicationId,
                    Integer.valueOf(equipment.getEquipmentStatus()),
                    format,
                    content,
                    equipment.getEquipmentId(),
                    status);
        }
    }
}
